<template>
  <div id="questionSubmitView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="questionId" label="题目" style="min-width: 240px">
        <a-input v-model="searchParams.title" placeholder="请输入" />
      </a-form-item>
      <a-form-item field="language" label="编程语言" style="min-width: 240px">
        <a-select v-model="searchParams.language" :style="{ width: '120px' }" placeholder="选择编程语言" allow-clear>
          <a-option v-for="item of languageList" :value="item.value" :label="item.text" />
        </a-select>
      </a-form-item>
      <a-form-item field="language" label="提交状态" style="min-width: 240px" >
        <a-select v-model="searchParams.submitState" :style="{ width: '240px' }" placeholder="选择提交状态" allow-clear>
          <a-option v-for="item of stateList" :value="item.text" :label="item.text" />
        </a-select>
      </a-form-item>
      <!-- <a-form-item>
        <a-button type="primary" @click="doSubmit">搜索</a-button>
      </a-form-item> -->
    </a-form>
    <a-divider size="0" />
    <a-table :ref="tableRef" :columns="columns" :data="dataList" column-resizable
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
    <template #user="{ record }">
        {{ record.user.nickname }}
      </template>
      <template #question="{ record }">
        <a-link v-on:click="toQuestionPage(record.question.id)" :hoverable="true">
          {{ record.question.title }}
        </a-link>
      </template>
      <template #judgeInfo="{ record }">
        <a-link v-on:click="toJudgeInfoPage(record.id)" :hoverable="true">
        {{ record.judgeInfo.message }}
        </a-link>
      </template>
      
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  SubmitControllerService,
  SubmitQueryRequest,
  CommonControllerService
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";

const tableRef = ref();
const router = useRouter();
const dataList = ref([]);
const total = ref(0);
const searchParams = ref<SubmitQueryRequest>({
  title: "",
  language: undefined,
  submitState: "",
  pageSize: 10,
  current: 1,
});

const languageList = ref(
  "" as any
)

const stateList = ref(
  "" as any
)

const loadData = async () => {
  const res = await SubmitControllerService.listSubmitByIdUsingPost(
    {
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    }
  );
  
  const res2 = await CommonControllerService.getEnumsListUsingGet('语言分类');
  languageList.value = res2.data

  const res3 = await CommonControllerService.getEnumsListUsingGet('提交结果');
  stateList.value = res3.data
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "提交者",
    slotName: "user",
  },
  {
    title: "题目",
    slotName: "question",
  },
  {
    title: "编程语言",
    dataIndex: "languageText",
  },
  {
    title: "状态",
    dataIndex: "statusText",
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
  },
  {
    title: "提交时间",
    dataIndex: "createTime",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

/**
 * 跳转到做题页面
 * @param question
 */
const toQuestionPage = (questionId: number) => {
  router.push({
    path: `/view/question/${questionId}`,
  });
};

/**
 * 跳转到提交页
 */
const toJudgeInfoPage = (submitId: number) => {
  router.push({
    path: `/view/judgeInfo/${submitId}`,
  });
}

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};
</script>

<style scoped>
#questionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
